﻿using BaseBll;
using BaseWebApi.App_Start;
using BaseWebApi.Common;
using BaseWebApi.Models;
using Common;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Net.Http;
using System.Web.Http;

namespace BaseWebApi.Controllers
{
    /// <summary>
    /// 供应商管理
    /// </summary>
    [ControllerGroup(groupName: "Supplier", useage: "采购管理-供应商管理")]
    public class SupplierController : BaseController
    {
        private JWTPlayloadInfo playload;

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页尺寸</param>
        /// <param name="number">供应商编号</param>
        /// <param name="name">供应商名称</param>
        /// <param name="contacts">联系人员</param>
        /// <param name="phone">联系方式</param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetPageList(int pageIndex, int pageSize, string number = "", string name = "", string contacts = "", string phone = "")
        {
            string strWhere = "";
            if (!string.IsNullOrEmpty(number))
            {
                strWhere += " and S_Number like '%" + number + "%'";
            }
            if (!string.IsNullOrEmpty(name))
            {
                strWhere += " and S_Name like '%" + name + "%'";
            }
            if (!string.IsNullOrEmpty(contacts))
            {
                strWhere += " and S_Contacts like '%" + contacts + "%'";
            }
            if (!string.IsNullOrEmpty(phone))
            {
                strWhere += " and S_Phone like '%" + phone + "%'";
            }

            DataTable dt = SupplierBll.Instance.GetPageList(pageSize, pageIndex, strWhere, "ID", out int records);
            int totalPages = PageHelper.GetPageCount(pageSize, records);
            return JsonHelper.SuccessJson(CommonOperation.DataTable2JArray(dt), pageIndex, totalPages, records);
        }

        /// <summary>
        /// 查询供应商列表
        /// </summary>
        /// <param name="number">供应商编号</param>
        /// <param name="name">供应商名称</param>
        /// <param name="contacts">联系人员</param>
        /// <param name="phone">联系方式</param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetList(string number = "", string name = "", string contacts = "", string phone = "")
        {
            string strWhere = "";
            if (!string.IsNullOrEmpty(number))
            {
                strWhere += " and S_Number like '%" + number + "%'";
            }
            if (!string.IsNullOrEmpty(name))
            {
                strWhere += " and S_Name like '%" + name + "%'";
            }
            if (!string.IsNullOrEmpty(contacts))
            {
                strWhere += " and S_Contacts like '%" + contacts + "%'";
            }
            if (!string.IsNullOrEmpty(phone))
            {
                strWhere += " and S_Phone like '%" + phone + "%'";
            }

            List<Supplier> list = SupplierBll.Instance.GetList(strWhere);

            return JsonHelper.SuccessJson(JsonHelper.ListToJArray(list));
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetModelById(int ID)
        {
            if (ID > 0)
            {
                Supplier model = SupplierBll.Instance.GetModelById(ID);
                if (model != null)
                {
                    return JsonHelper.SuccessJson(JsonHelper.ModelToJObject(model));
                }
                else
                {
                    return JsonHelper.FailJson("数据不存在");
                }
            }
            else
            {
                return JsonHelper.FailJson("参数错误");
            }
        }

        /// <summary>
        /// 新增供应商
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage AddSupplier([FromBody] Supplier model)
        {
            playload = (JWTPlayloadInfo)Request.Properties["playload"];

            try
            {
                //验证各字段合法性
                string msg = "";
                bool isOk = SupplierBll.Instance.VerifyModel(model, ref msg);
                if (isOk)
                {
                    bool res = SupplierBll.Instance.AddSupplier(model);
                    if (res)
                    {
                        new LogHelper().Info(new LogContent(Utils.GetNextID(), playload.LoginIP, playload.UserName, Constant.SysModule.Supplier, "新增供应商", Constant.ActionEnum.Add, Constant.StatusEnum.Success));
                        return JsonHelper.SuccessJson("新增成功");
                    }
                    else
                    {
                        new LogHelper().Info(new LogContent(Utils.GetNextID(), playload.LoginIP, playload.UserName, Constant.SysModule.Supplier, "新增供应商", Constant.ActionEnum.Add, Constant.StatusEnum.Fail));
                        return JsonHelper.FailJson("新增失败");
                    }
                }
                else
                {
                    return JsonHelper.FailJson(msg);
                }
            }
            catch (Exception ex)
            {
                new LogHelper().Error(new LogContent(Utils.GetNextID(), playload.LoginIP, playload.UserName, Constant.SysModule.Supplier, "新增供应商:" + ex.Message, Constant.ActionEnum.Add, Constant.StatusEnum.Exception), ex);
                throw ex;
            }
        }

        /// <summary>
        /// 修改供应商
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage UpdateSupplier([FromBody] Supplier model)
        {
            playload = (JWTPlayloadInfo)Request.Properties["playload"];
            if (model.ID <= 0)
            {
                return JsonHelper.ErrorJson("参数错误");
            }
            try
            {
                bool isExist = SupplierBll.Instance.IsExist(" and ID=" + model.ID);
                if (!isExist)
                {
                    return JsonHelper.ErrorJson("不存在该数据");
                }
                //验证各字段合法性
                string msg = "";
                bool isOk = SupplierBll.Instance.VerifyModel(model, ref msg);
                if (isOk)
                {
                    bool res = SupplierBll.Instance.UpdateSupplier(model);
                    if (res)
                    {
                        new LogHelper().Info(new LogContent(Utils.GetNextID(), playload.LoginIP, playload.UserName, Constant.SysModule.Supplier, "修改供应商，ID:" + model.ID, Constant.ActionEnum.Edit, Constant.StatusEnum.Success));
                        return JsonHelper.SuccessJson("修改成功");
                    }
                    else
                    {
                        new LogHelper().Info(new LogContent(Utils.GetNextID(), playload.LoginIP, playload.UserName, Constant.SysModule.Supplier, "修改供应商，ID:" + model.ID, Constant.ActionEnum.Edit, Constant.StatusEnum.Fail));
                        return JsonHelper.FailJson("修改失败");
                    }
                }
                else
                {
                    return JsonHelper.FailJson(msg);
                }
            }
            catch (Exception ex)
            {
                new LogHelper().Error(new LogContent(Utils.GetNextID(), playload.LoginIP, playload.UserName, Constant.SysModule.Supplier, "修改供应商，ID:" + model.ID + ex.Message, Constant.ActionEnum.Edit, Constant.StatusEnum.Exception), ex);
                throw ex;
            }
        }

        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage DeleteSupplier(int id)
        {
            playload = (JWTPlayloadInfo)Request.Properties["playload"];
            if (id <= 0)
            {
                return JsonHelper.ErrorJson("参数错误");
            }
            try
            {
                bool isExist = SupplierBll.Instance.IsExist(" and ID=" + id);
                if (!isExist)
                {
                    return JsonHelper.ErrorJson("不存在该数据");
                }
                bool result = SupplierBll.Instance.DeleteSupplier(id.ToString());
                if (result)
                {
                    new LogHelper().Info(new LogContent(Utils.GetNextID(), playload.LoginIP, playload.UserName, Constant.SysModule.Supplier, "删除供应商,ID：" + id, Constant.ActionEnum.Delete, Constant.StatusEnum.Success));
                    return JsonHelper.SuccessJson("删除成功");
                }
                else
                {
                    new LogHelper().Info(new LogContent(Utils.GetNextID(), playload.LoginIP, playload.UserName, Constant.SysModule.Supplier, "删除供应商,ID：" + id, Constant.ActionEnum.Delete, Constant.StatusEnum.Fail));
                    return JsonHelper.SuccessJson("删除失败");
                }
            }
            catch (Exception ex)
            {
                new LogHelper().Error(new LogContent(Utils.GetNextID(), playload.LoginIP, playload.UserName, Constant.SysModule.Supplier, "删除供应商,ID：" + id + ex.Message, Constant.ActionEnum.Delete, Constant.StatusEnum.Exception), ex);
                throw ex;
            }
        }
    }
}